package sdk.weinxin.beans.ticket;

import com.fasterxml.jackson.annotation.JsonProperty;
import sdk.weinxin.format.JsonObjectMapper;

public class Ticket {

    private String ticket;

    @JsonProperty("expires_in")
    private long expiresIn;

    private long expiresTill;

    public static Ticket fromJson(String json) {
        return JsonObjectMapper.defaultMapper().fromJson(json, Ticket.class);
    }

    public String getTicket() {
        return ticket;
    }

    public void setTicket(String ticket) {
        this.ticket = ticket;
    }

    public long getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(long expiresIn) {
        this.expiresIn = expiresIn;
        this.expiresTill = System.currentTimeMillis() + (expiresIn * 1000) - 60000;
    }

    public long getExpiresTill() {
        return expiresTill;
    }

    public boolean expired() {
        return System.currentTimeMillis() > expiresTill;
    }
}
